<template>
  <div class="login-container">
    <div class="banner">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
               label-position="left">
        <h3 class="title">修改密码</h3>
        <el-form-item prop="mobile">

          <el-input v-model="loginForm.mobile" name="mobile" type="text" auto-complete="on" placeholder="用户名(手机号)">
            <template slot="prepend">
              <span class="svg-container">
                <svg-icon icon-class="user"/>
              </span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            placeholder="请输入新密码"
            @keyup.enter.native="handleLogin">
            <template slot="prepend">
              <span class="svg-container">
                <svg-icon icon-class="password"/>
              </span>
            </template>
          </el-input>
          <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye"/>
        </span>
        </el-form-item>
        <el-form-item prop="verifyCode">
          <el-input v-model="loginForm.verifyCode" auto-complete="on" placeholder="验证码">
            <template slot="append">
              <el-button :disabled="code_status" @click="getCode" style="width: 120px;">{{code_text}}</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-top: 20px">
          <el-form-item style="margin-top: 0px">
            <el-button :loading="loading" type="primary"
                       style="width:100%;background-color: #3ea751;border-color: #3ea751;"
                       @click.native.prevent="handleLogin">
              修改密码
            </el-button>
            <!--<span style=" width: 100%;text-align: center;display: block;line-height: 20px;">or</span>-->
            <el-button style="width:100%;background: #2b2b36;position: relative;height: 40px;margin: 10px auto;" @click.native.prevent="">
              <router-link to="login" class="btn-link edit-btn"
                           style="position: absolute;width: 100%;height: 100%;left: 0;top: 0;line-height: 40px;">
                登陆
              </router-link>
            </el-button>

          </el-form-item>
        </el-form-item>
      </el-form>
      <div class="clearboth" style="clear: both"></div>
    </div>
    <div class="copyright" style="text-align: center;color: #FFF"><span>浙ICP备17056522号-1  copyright 2017-2019 E招招聘</span></div>

  </div>
</template>

<script>
  import {isvalidUsername} from '@/utils/validate'
  import {retrieve} from '@/api/web/auth'
  import {Message} from 'element-ui'

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback()
        } else {
          callback()
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位!'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          mobile: '',
          verifyCodeId: '',
          password: '',
          verifyCode: ''

        },
        loginRules: {
          mobile: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePass}],
          verifyCode: [{required: true, trigger: 'blur', message: '验证码为必填项!'}],
        },
        code_text: '获取验证码',
        code_status: false,
        loading: false,
        pwdType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            let params = {
              verifyCodeId: this.loginForm.verifyCodeId,
              verifyCode: this.loginForm.verifyCode,
              password: this.loginForm.password
            }
            retrieve(params).then(res => {
              this.loading = false
              Message.success("密码重置成功")
              this.$router.push({path: 'login'})
            }).catch(err => {

            })

          } else {
            console.log('error submit!!')
            return false
          }
        })
      }, 
      getCode() {
        // 校验手机号合法性
       var mobile = this.loginForm.mobile


        this.code_status = true
        let time = 60 * 10;
        this.code_text = time
        let _this = this
        let timer = setInterval(() => {
          if (time >= 1) {
            _this.code_text = time -= 1
          } else {
            clearInterval(timer)
            _this.code_status = false
            _this.code_text = "获取验证码"
          }
        }, 1000)
        
        _this.$fetch(_this.$api.findBackPwd, {
            mobile: mobile
        }).then(res => {
            if (res.code != 0) {
            Message.error("获取验证码失败" + res.msg)
          } else {
            _this.loginForm.verifyCodeId = res.data
            Message.success("获取验证码成功")
          }  
        }).catch(err => {
          Message.error("获取验证码失败" + res.msg)
          clearInterval(timer)
          _this.code_status = false
          _this.code_text = "获取验证码"
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $light_gray: #eee;
  .banner {
    /*background: #080818 url(https://res.wx.qq.com/mpres/zh_CN/htmledition/pages/login/loginpage/images/bg_banner412d83.png);*/
    left: 0;
    right: 0;
    width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 12vw auto;
    background-repeat: no-repeat;
  }

  .el-form-item {
    /*margin-bottom: 0;*/

  }

  .el-form-item__content {
    line-height: normal;
    margin: 2px 0;
  }

  .el-input-group__prepend {
    background-color: #2b2b36;
    border: 1px solid #9595ad;
  }

  .el-input__inner {
    background-color: #2b2b36;
    border: 1px solid #9595ad;
  }

  .el-input-group__append {
    background-color: transparent;
    border: 1px solid #9595ad;
    color: #dee4e7;
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background: $bg url("../../assets/bg1.jpg") center/100% 100% no-repeat;
    .login-form {
      width: 420px;
      padding: 35px 35px 15px 35px;
      background: #2b2b36;
      position: relative;
      margin: auto;
      /*float: right;*/
      /*margin-right: 20%;*/
      /*height: 290px;*/
      /*margin-bottom: 100px;*/
      border-radius: 5px;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: #dee4e7;
      margin: 0px auto 10px auto;
      text-align: center;
      font-weight: bold;

    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
